package org.paradise.dms.web.action.aptview;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.Dormitory;
import org.paradise.dms.services.DormitoryService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 房态浏览中宿舍视图
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-18 下午09:38:11 李双江（paradise.lsj@gmail.com）created
 */
@RemoteProxy(name = "dormitoryViewAction")
@Service
public class DormitoryViewAction extends DMSBaseAction {
	private static Logger log = Logger.getLogger(DormitoryViewAction.class);
	@Autowired
	private DormitoryService dormitoryService;
	@Autowired
	private PagerService pagerService;
	private Pager pager;
	private List<Dormitory> dormList;

	private String apartmentid;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;
	private String dormitoryfloorno;
	private String apartmentname;
	private String dormitoryname;

	public String getDormitoryfloorno() {
		return dormitoryfloorno;
	}

	public void setDormitoryfloorno(String dormitoryfloorno) {
		this.dormitoryfloorno = dormitoryfloorno;
	}

	public String getDormitoryname() {
		return dormitoryname;
	}

	public void setDormitoryname(String dormitoryname) {
		this.dormitoryname = dormitoryname;
	}

	public String getApartmentname() {
		return apartmentname;
	}

	public void setApartmentname(String apartmentname) {
		this.apartmentname = apartmentname;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getApartmentid() {
		return apartmentid;
	}

	public void setApartmentid(String apartmentid) {
		this.apartmentid = apartmentid;
	}

	public List<Dormitory> getDormList() {
		return dormList;
	}

	public void setDormList(List<Dormitory> dormList) {
		this.dormList = dormList;
	}

	public DormitoryService getDormitoryService() {
		return dormitoryService;
	}

	public void setDormitoryService(DormitoryService dormitoryService) {
		this.dormitoryService = dormitoryService;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 4609043279377504608L;

	/**
	 * 
	 * Description: 根据apartmentid查询出所有宿舍
	 * 
	 * @Version1.0 2009-3-18 下午09:40:39 李双江（paradise.lsj@gmail.com）创建
	 * @param apartmentid
	 * @return
	 */
	
	public String listAllDormitoryByApartmentID() {
		return SUCCESS;
	}
	

	/**
	 * 
	 * Description: 根据页面选择的floorno来列出所有的该层的宿舍
	 * 
	 * @Version1.0 2009-3-18 上午01:43:31 李双江（paradise.lsj@gmail.com）创建
	 * @param floorno
	 *            select列表框的楼层数
	 * @return
	 */
	public String listDormitoryByFloorNo(String floorno) {
		return SUCCESS;
	}


	/**
	 * 
	 * Description: 通过公寓id和楼层号外加宿舍名称查询特定宿舍信息
	 * 
	 * @Version1.0 2009-3-20 上午03:12:36 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws DaoException
	 * @throws SQLException
	 */
	/*
	@SuppressWarnings("unchecked")
	public String findDormitoryByApartmentIDAndFloornoOrDormname() throws UnsupportedEncodingException, DaoException, SQLException {
		int totalRow = 1; // 赋初始值
		String aprtid = this.getApartmentid();
		String dorm_name = this.getDormitoryname();
		String dorm_floor_no = this.getDormitoryfloorno();
		if (("".equals(this.getDormitoryname()) || this.getDormitoryname() == null)) { // 宿舍名没填写
			try {
				totalRow = dormitoryService.getAFloorRows(aprtid, dorm_floor_no);
				// log.info(aprtid + "第" + dorm_floor_no + "totalRows:" +
				// totalRow);
			} catch (DaoException e) {
				log.error("DMS_error:更具apartmentid查询记录时出错");
				log.error("错误原因:" + e);
			}
		}
		pager = pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow);
		this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
		this.setTotalRows(String.valueOf(totalRow));
		dormList = dormitoryService.findDormitoryByApartmentNameAndFloornoOrDormname(aprtid, dorm_floor_no, dorm_name, pager.getPageSize(), pager.getStartRow());
		if (dormList != null && dormList.size() > 0) {
			String[] _dormitoryid = new String[dormList.size()];
			for (int m = 0; m < dormList.size(); m++) {
				_dormitoryid[m] = dormList.get(m).getDormitoryid()+"";
			}
			List inolst = dormitoryService.findDormitoryDisplayPictureSpec(_dormitoryid);
			for (int j = 0; j < _dormitoryid.length; j++) {
				dormList.get(j).setDormitorybednum(Integer.parseInt(inolst.get(j) + ""));
			}
			return SUCCESS;
		}
		return INPUT;
	}
	*/
	public String getDormByDormId(int dormitoryid) {
		JSONObject jsonObject = JSONObject.fromObject(dormitoryService.getDormByDormId(dormitoryid));
		return jsonObject.toString();
	}
	
	
	/**
	 * dormsearchviewer.jsp页面的宿舍高级查询
	 * 返回值每一行内的元素顺序为dormid,dormname,dormfunction,dormconcretefunction,dormtype,dormgender,usedbed
	 */
	public String[][] advancedDormSearch(String sql){
		return dormitoryService.advancedDormSearch(sql);
	}
	public int getAdvancedDormSearchTotalNumber(String sql){
		int a =  dormitoryService.getAdvancedDormSearchTotalNumber(sql);
		return a;
	}
	
	/**
	 * dormsearchviewer.jsp页面对宿舍信息的更新
	 * @param key  与数据库的字段名有映射关系
	 * @param value 字段值
	 * @param id  宿舍id
	 * @return
	 */
	public String updateDormInfo(String key ,String value,String id){
		return dormitoryService.updateDormInfo(key,value,id);
	}
	public String[][]  getAllDormitoryConcreteFunction(){
		return dormitoryService.getAllDormitoryConcreteFunction();
	}
	
}
